//index.js
//获取应用实例
const app = getApp()

Page({


  /**
   * 页面的初始数据
   */
  data: {
    clearshow:false,
    active: true,
    search: "",
    show: true,
    img: app.globalData.img,
    swpImg: [],
    // false: false,
    history: [],
    inputvalue:null,
    List: [],

    nav: [{
      title: '全部分类',
      img: '../image/01.png',
      url: "/pages/spacetype/spacetype"
    },
    {
      title: '全屋定制',
      img: '../image/02.png',
      url: "/pages/room/room"
    },
    {
      title: '优选单品',
      img: '../image/03.png',
      url:'/pages/goodselect/goodselect'
    },
    {
      title: '真实案例',
      img: '../image/04.png',
      url: '/pages/case/case'
    }
    ],

    productList: [],
    // productList: [{
    //   ProductPubTypeId: 5,
    //   ProductPubTypeName: "床",
    //   Product: [],
    //   more: '../image/bad.png',
    // }, {
    //   ProductPubTypeId: 6,
    //   ProductPubTypeName: "沙发",
    //     Product: [],
    //   more: '../image/sofa.png',
    // }, {
    //   ProductPubTypeId: 7,
    //   ProductPubTypeName: "柜类",
    //     Product: [],
    //   more: '../image/cab.png',
    // }, {
    //   ProductPubTypeId: 8,
    //   ProductPubTypeName: "桌类",
    //     Product: [],
    //   more: '../image/table.png',
    // }, {
    //   ProductPubTypeId: 15,
    //   ProductPubTypeName: "椅子",
    //     Product: [],
    //   more: '../image/chair.png',
    // }],


  },
  search: function () {
    this.setData({
      active: !this.data.active
    })
  },
  closeActive: function () {
    this.setData({
      active: !this.data.active
    })
  },
  changeText: function (e) {
    let name = e.currentTarget.dataset.name;
    let nameMap = {}
    nameMap[name] = e.detail && e.detail.value
    this.setData(nameMap)
  },
  // 轮播图
  swiper: function () {
    var that = this
    app.req({
      url: 'ImageInfo?imageGroupId=30',
      method: 'GET',
      success: function (res) {
        if (res.data.Code == true) {
          that.setData({
            swpImg: res.data.Result.Items
          })
        }

      }
    })
  },

  swiperpic: function (e) {
    wx.navigateTo({
      url: '/pages/prodetail/prodetail?id=' + e.currentTarget.dataset.index
    })
  },

// 12
  typeList: function () {
    var that = this
    app.req({
      url:"Product/ProductType",
      success(res){
        var arr=[]
        for(let i=0;i<4;i++){
          var obj = res.data.Result[i]
          arr.push(obj)
        }
        if(arr.length>0){
          for (let i = 0; i < arr.length; i++) {
            app.req({
              url: 'Product/Type/Product/' + arr[i].ProductPubTypeId,
              success: function (res) {
                arr[i].Product = res.data.Result
                that.setData({
                  productList: arr
                })
              }
            })
          }
        }
        that.setData({
          productList: arr
        })
      }
    })
  },
  // productList: function (obj) {
  //   console.log(obj)
  //   var obj = this.data.productList
  //   for (let i = 0; i < obj.length; i++) {
  //     console.log(obj[i])
  //     app.req({
  //       url: 'Product/Type/Product/' + obj[i].ProductPubTypeId,
  //       success: function (res) {
  //         console.log(res)
  //         obj[i].Product = res.data.Result
  //         that.setData({
  //           productList: obj
  //         })
  //       }
  //     })
  //   }
  // },
  // productList: function () {
  //   var that = this
  //   var obj = this.data.productList
  //  for(let i=0;i<obj.length;i++){
  //    app.req({
  //      url: 'Product/Type/Product/' + obj[i].ProductPubTypeId,
  //      success:function(res) {
  //        obj[i].Product = res.data.Result
  //       that.setData({
  //         productList: obj
  //       })
  //      }
  //    })
  //  }
  // },
  //  获取input的值
  getvalue:function(e) {
    this.setData({
      inputvalue:e.detail.value,
      clearshow:true,
    })
    if (e.detail.value == '') {
      this.setData({
        inputvalue: e.detail.value,
        clearshow: false,
      })

    }
  },

  // 键盘搜索事件
  sousuo: function (e) {
    this.data.history.push(e.detail.value)
    var newhistory = []
    for (var i = 0; i < this.data.history.length; i++) {
      if (newhistory.indexOf(this.data.history[i]) === -1) {
        newhistory.push(this.data.history[i])
      }
    }
    if (newhistory == '' && e.detail.value == '') {
      wx.showToast({
        title: '请输入关键字',
        icon: 'none',
        duration: 2000
      })
    } else {
      // 要跳转的页面记得带参数
      wx.navigateTo({
        url: '/pages/search/search?user_id=' + e.detail.value
      })
    }
    wx.setStorageSync('history', newhistory)
    this.setData({
      history: newhistory,
      show: true,
      clearshow:true
    })
  },



  //  清楚搜索关键字
  clearvalue:function() {
    this.setData({
      search:'',
      clearshow: false
    })
  },

  // 获取历史纪录
  gethistory: function () {
    this.setData({
      history: wx.getStorageSync('newhistory') || []
    })
  },

  //清空搜索纪录
  clearHistory: function () {
    this.setData({
      history: [],
      show: false
    })
    wx.setStorageSync('newhistory', [])
  },
  // 点击历史记录调转
  searchjump: function (e) {
    var that = this
    wx.navigateTo({
      url: '/pages/search/search?user_id=' + e.currentTarget.id
    })
    that.setData({
      active: !this.data.active
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.swiper()
    this.typeList()
    this.gethistory()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})